/* 
 * Copyright (c) 2022, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.boot.sys.service;

import java.io.Serializable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cory.boot.sys.entity.SysDictCode;
import com.cory.boot.sys.entity.SysDictValue;
import com.cory.boot.sys.mapper.SysDictCodeMapper;
import com.cory.boot.sys.mapper.SysDictValueMapper;

/**
 * @author xiongcong
 * @date 2022-01-13
 */
@Service
public class SysDictService {
    @Autowired
    private SysDictCodeMapper sysDictCodeMapper;
    @Autowired
    private SysDictValueMapper sysDictValueMapper;
    
    public List<SysDictCode> dictCodeList(SysDictCode param) {
        return sysDictCodeMapper.list(param);
    }
    
    public SysDictCode getDictCodeById(Serializable id) {
        return sysDictCodeMapper.selectById(id);
    }
    
    public boolean updateDictCodeById(SysDictCode entity) {
        sysDictCodeMapper.updateById(entity);
        return true;
    }
    
    public boolean saveDictCode(SysDictCode entity) {
        sysDictCodeMapper.insert(entity);
        return true;
    }
    
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDictCodeById(String id) {
        sysDictCodeMapper.deleteById(id);
        sysDictValueMapper.deleteByDictCode(id);
        return true;
    }
    
    public List<SysDictValue> dictValueList(SysDictValue param) {
        return sysDictValueMapper.list(param);
    }
    
    public SysDictValue getDictValue(String dictCode, String dictKey) {
        return sysDictValueMapper.getDictValue(dictCode, dictKey);
    }

    public String getDictKey(String dictCode, String dictValue) {
        return sysDictValueMapper.getDictKey(dictCode, dictValue);
    }
    
    public boolean updateDictValue(SysDictValue entity) {
        sysDictValueMapper.update(entity);
        return true;
    }
    
    public boolean saveDictValue(SysDictValue entity) {
        sysDictValueMapper.insert(entity);
        return true;
    }
    

    public boolean deleteDictValue(String dictCode, List<String> dictKeys) {
        sysDictValueMapper.delete(dictCode, dictKeys);
        return true;
    }
    
    public boolean sortDictValue(String dictCode, List<SysDictValue> list) {
        for (SysDictValue sysDictValue : list) {
            sysDictValueMapper.updateSort(dictCode, sysDictValue.getDictKey(), sysDictValue.getSort());
        }
        return true;
    }
    
    public boolean updateDictValueState(String dictCode, String dictKey, Integer state) {
        sysDictValueMapper.updateState(dictCode, dictKey, state);
        return true;
    }

}
